#!/bin/bash
if [ $# -lt 1 ] ; then
    echo "bash iot-client-installer %deviceId%"
    exit 1;
fi
ulimit -n 65535
echo ulimit -n 65535 >> /etc/profile
source /etc/profile
echo 10000 > /proc/sys/net/core/somaxconn
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 0 > /proc/sys/net/ipv4/tcp_syncookies
echo "* soft nofile 65565" >> /etc/security/limits.conf
echo "* hard nofile 65565" >> /etc/security/limits.conf
yum -y install epel-release
yum -y install supervisor wget
chmod 600 /etc/inittab
cd /usr/local/src
if [ ! -f "/usr/local/src/iotClientInstaller.tar.gz" ];then
    wget -t 0 -c "https://res.codedance.top/iotClientInstaller.tar.gz"
fi
mkdir -p /data
tar -zxf iotClientInstaller.tar.gz -C /data
crontab /data/app/crontab
mkdir -p /opt
mkdir -p /root/logs/iot
if [ ! -f "/usr/local/src/jdk-8u161-linux-x64.tar.gz" ];then
    wget -t 0 -c "https://res.codedance.top/jdk-8u161-linux-x64.tar.gz"
fi
tar -zxf jdk-8u161-linux-x64.tar.gz -C /opt
if [ -f "/etc/supervisord.d/iot-client.ini" ];then
    rm -f /etc/supervisord.d/iot-client.ini
fi
touch /etc/supervisord.d/iot-client.ini
cat > /etc/supervisord.d/iot-client.ini << EOF
[program:iot-client]
user=root
command=/opt/jdk1.8.0_161/bin/java -jar /data/app/iotClient.jar -h 101.200.83.212 -d %deviceId
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/root/logs/iotp/out.log
stopasgroup=true
killasgroup=true
EOF
sed -i "s/%deviceId/$1/" /etc/supervisord.d/iot-client.ini
systemctl enable supervisord
systemctl restart supervisord
supervisorctl reload 2>&1 > /dev/null
supervisorctl start iot-client
echo "Success"
